﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI.WebControls;
using ASPNETVN.PORTAL.Components.UI;
using ASPNETVN.PORTAL.Modules.ACTP.Components;
using ASPNETVN.PORTAL.Components.Helpers;

namespace ASPNETVN.PORTAL.Modules.ACTP.Controls
{
    public partial class AddEmployeeToProject : AddEditItemControl
    {
        #region Extra Properties
        private int _TeamID;
        public int TeamID
        {
            get
            {
                if (_TeamID == 0)
                {
                    if (HttpContext.Current != null && Request.Params["TeamID"] != null)
                        _TeamID = Int32.Parse(Request.Params["TeamID"]);
                }
                return _TeamID;
            }
            set { _TeamID = value; }
        }

        private int _ProjectID;
        public int ProjectID
        {
            get
            {
                if (_ProjectID == 0)
                {
                    if (HttpContext.Current != null && Request.Params["ProjectID"] != null)
                        _ProjectID = Int32.Parse(Request.Params["ProjectID"]);
                }
                return _ProjectID;
            }
            set { _ProjectID = value; }
        }

        #endregion

        protected void Page_Load(object sender, EventArgs e)
        {
            if (IsPostBack == false)
            {
                // Certifications.
                ddlCertification.DataSource = Certification.SelectCollectionAll(this.ModuleID);
                ddlCertification.DataBind();

                // Project Detail Control.
                ProjectDetailControl1.ProjectID = this.ProjectID;

                // Available Employees.
                chkAvailableEmployees.DataSource = Employee.SelectCollectionBy_ProjectID_CertificationID_Except_AddedInProject(this.ModuleID, this.ProjectID, 0).Select(x => new { x.EmployeeID, Title = "(Code: " + x.EmployeeCode + ") " + x.FirstName + " " + x.LastName });
                chkAvailableEmployees.DataBind();

                // Exist Employees
                chkExistEmployees.DataSource = Employee.SelectCollectionBy_ProjectID(this.ModuleID, this.ProjectID).Select(x => new { x.EmployeeID, Title = "(Code: " + x.EmployeeCode + ") " + x.FirstName + " " + x.LastName });
                chkExistEmployees.DataBind();

                // ProjectManager.
                IList<ProjectEmployee> projectEmployees = ProjectEmployee.SelectCollectionBy_ProjectID(this.ProjectID);
                ProjectEmployee manager = projectEmployees.FirstOrDefault(x => x.IsProjectManager);
                IList<Employee> employees = new List<Employee>();
                if (manager != null)
                {
                    employees.Add(Employee.Load(manager.EmployeeID));
                }
                chkProjectManager.DataSource = employees.Select(x => new { x.EmployeeID, Title = "(Code: " + x.EmployeeCode + ") " + x.FirstName + " " + x.LastName });
                chkProjectManager.DataBind();
            }
        }

        protected void btnAddEmployee_Click(object sender, System.Web.UI.ImageClickEventArgs e)
        {
            foreach (ListItem item in chkAvailableEmployees.Items)
            {
                if (item.Selected)
                {
                    ProjectEmployee projectEmployee = new ProjectEmployee();
                    projectEmployee.ModuleID = this.ModuleID;
                    projectEmployee.TeamID = this.TeamID;
                    projectEmployee.ProjectID = this.ProjectID;
                    projectEmployee.EmployeeID = Convert.ToInt32(item.Value);
                    projectEmployee.Insert();
                }
            }

            // Refresh 02 CheckBoxList.
            ddlCertification.SelectedIndex = 0;
            chkAvailableEmployees.DataSource = Employee.SelectCollectionBy_ProjectID_CertificationID_Except_AddedInProject(this.ModuleID, this.ProjectID, 0).Select(x => new { x.EmployeeID, Title = "(Code: " + x.EmployeeCode + ") " + x.FirstName + " " + x.LastName });
            chkAvailableEmployees.DataBind();

            chkExistEmployees.DataSource = Employee.SelectCollectionBy_ProjectID(this.ModuleID, this.ProjectID).Select(x => new { x.EmployeeID, Title = "(Code: " + x.EmployeeCode + ") " + x.FirstName + " " + x.LastName });
            chkExistEmployees.DataBind();
        }

        protected void btnRemoveEmployee_Click(object sender, System.Web.UI.ImageClickEventArgs e)
        {
            foreach (ListItem item in chkExistEmployees.Items)
            {
                if (item.Selected)
                {
                    ProjectEmployee.DeleteProjectEmployee(this.TeamID, this.ProjectID, Convert.ToInt32(item.Value));
                }
            }


            // Refresh 02 CheckBoxList.
            ddlCertification.SelectedIndex = 0;
            chkAvailableEmployees.DataSource = Employee.SelectCollectionBy_ProjectID_CertificationID_Except_AddedInProject(this.ModuleID, this.ProjectID, 0).Select(x => new { x.EmployeeID, Title = "(Code: " + x.EmployeeCode + ") " + x.FirstName + " " + x.LastName });
            chkAvailableEmployees.DataBind();

            chkExistEmployees.DataSource = Employee.SelectCollectionBy_ProjectID(this.ModuleID, this.ProjectID).Select(x => new { x.EmployeeID, Title = "(Code: " + x.EmployeeCode + ") " + x.FirstName + " " + x.LastName });
            chkExistEmployees.DataBind();

            // ProjectManager.
            IList<ProjectEmployee> projectEmployees = ProjectEmployee.SelectCollectionBy_ProjectID(this.ProjectID);
            ProjectEmployee manager = projectEmployees.FirstOrDefault(x => x.IsProjectManager);
            IList<Employee> employees = new List<Employee>();
            if (manager != null)
            {
                employees.Add(Employee.Load(manager.EmployeeID));
            }
            chkProjectManager.DataSource = employees.Select(x => new { x.EmployeeID, Title = "(Code: " + x.EmployeeCode + ") " + x.FirstName + " " + x.LastName });
            chkProjectManager.DataBind();
        }

        protected void ddlCertification_SelectedIndexChanged(object sender, EventArgs e)
        {
            chkAvailableEmployees.Items.Clear();
            int certificationID = Convert.ToInt32(ddlCertification.SelectedValue);
            chkAvailableEmployees.DataSource = Employee.SelectCollectionBy_ProjectID_CertificationID_Except_AddedInProject(this.ModuleID, this.ProjectID, certificationID).Select(x => new { x.EmployeeID, Title = "(Code: " + x.EmployeeCode + ") " + x.FirstName + " " + x.LastName });
            chkAvailableEmployees.DataBind();
        }

        protected void btnCancel_Click(object sender, EventArgs e)
        {
            string url = HttpUrlBuilder.BuildModulePageUrl(this.ModuleID, this.ProjectID, ModulePages.ProjectDetail);
            Response.Redirect(url);
        }

        protected void btnAddProjectManager_Click(object sender, System.Web.UI.ImageClickEventArgs e)
        {
            if (chkProjectManager.Items.Count > 0)
            {
                lblMessage.Text = "Only one project manager in a project";
                return;
            }

            int count = chkExistEmployees.Items.Cast<ListItem>().Count(item => item.Selected);

            if (count > 1)
            {
                lblMessage.Text = "Only one project manager in a project";
                return;
            }

            foreach (ListItem item in chkExistEmployees.Items)
            {
                if (item.Selected)
                {
                    ProjectEmployee projectEmployee = ProjectEmployee.Load(this.TeamID, this.ProjectID, Convert.ToInt32(item.Value));
                    projectEmployee.IsProjectManager = true;
                    projectEmployee.Update();
                }
            }

            // Refresh 02 CheckBoxList.
            ddlCertification.SelectedIndex = 0;
            chkAvailableEmployees.DataSource = Employee.SelectCollectionBy_ProjectID_CertificationID_Except_AddedInProject(this.ModuleID, this.ProjectID, 0).Select(x => new { x.EmployeeID, Title = "(Code: " + x.EmployeeCode + ") " + x.FirstName + " " + x.LastName });
            chkAvailableEmployees.DataBind();

            chkExistEmployees.DataSource = Employee.SelectCollectionBy_ProjectID(this.ModuleID, this.ProjectID).Select(x => new { x.EmployeeID, Title = "(Code: " + x.EmployeeCode + ") " + x.FirstName + " " + x.LastName });
            chkExistEmployees.DataBind();

            // ProjectManager.
            IList<ProjectEmployee> projectEmployees = ProjectEmployee.SelectCollectionBy_ProjectID(this.ProjectID);
            ProjectEmployee manager = projectEmployees.FirstOrDefault(x => x.IsProjectManager);
            if (manager != null)
            {
                IList<Employee> employees = new List<Employee>();
                employees.Add(Employee.Load(manager.EmployeeID));

                chkProjectManager.DataSource = employees.Select(x => new { x.EmployeeID, Title = "(Code: " + x.EmployeeCode + ") " + x.FirstName + " " + x.LastName });
                chkProjectManager.DataBind();
            }
        }

        protected void btnRemoveProjectManager_Click(object sender, System.Web.UI.ImageClickEventArgs e)
        {
            foreach (ListItem item in chkProjectManager.Items)
            {
                if (item.Selected)
                {
                    ProjectEmployee projectEmployee = ProjectEmployee.Load(this.TeamID, this.ProjectID, Convert.ToInt32(item.Value));
                    projectEmployee.IsProjectManager = false;
                    projectEmployee.Update();
                }
            }

            // Refresh 02 CheckBoxList.
            ddlCertification.SelectedIndex = 0;
            chkAvailableEmployees.DataSource = Employee.SelectCollectionBy_ProjectID_CertificationID_Except_AddedInProject(this.ModuleID, this.ProjectID, 0).Select(x => new { x.EmployeeID, Title = "(Code: " + x.EmployeeCode + ") " + x.FirstName + " " + x.LastName });
            chkAvailableEmployees.DataBind();

            chkExistEmployees.DataSource = Employee.SelectCollectionBy_ProjectID(this.ModuleID, this.ProjectID).Select(x => new { x.EmployeeID, Title = "(Code: " + x.EmployeeCode + ") " + x.FirstName + " " + x.LastName });
            chkExistEmployees.DataBind();

            // ProjectManager.
            IList<ProjectEmployee> projectEmployees = ProjectEmployee.SelectCollectionBy_ProjectID(this.ProjectID);
            ProjectEmployee manager = projectEmployees.FirstOrDefault(x => x.IsProjectManager);
            IList<Employee> employees = new List<Employee>();
            if (manager != null)
            {
                employees.Add(Employee.Load(manager.EmployeeID));
            }
            chkProjectManager.DataSource = employees.Select(x => new { x.EmployeeID, Title = "(Code: " + x.EmployeeCode + ") " + x.FirstName + " " + x.LastName });
            chkProjectManager.DataBind();
        }
    }
}